package Dao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.swing.JOptionPane;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import Modelo.Telefones;


public class Dao {
 private SessionFactory sessionFactory;
 private Session session;
 private Telefones t;
 private int id;
 
 
 public void setCodId(int codId) {
  this.id = codId;
 }
 public Telefones gett() {
  return t;
 }
 public void setUser(Telefones t) {
  this.t = t;
 }
 private void abrirTransacao() {
  sessionFactory = new Configuration().configure().buildSessionFactory();
  session = sessionFactory.openSession();
  session.beginTransaction();
 }
 private void fecharTransacao() {
  session.getTransaction().commit();
  session.close();
 }
 public List<Telefones> getListar() {
  List<Telefones> lista = new ArrayList<Telefones>();
  try {
   abrirTransacao();
   Query query = session.createQuery("Telefones");
   lista = query.list();
   fecharTransacao();
  } catch (Throwable e) {
  }
  return lista;
 }
 
 public boolean getObter() {
	 try {
	 abrirTransacao();
	 t = (Telefones)session.get(Telefones.class, id);
	 fecharTransacao();
	 return true;
	 } catch (Throwable e) {
	 System.out.println("ID: " + e.getMessage());
	 return false;
	 }
	 }
	 
 
 public boolean getIncluir( Telefones tel) {
	
	try {
	 abrirTransacao();
	 session.save(tel);
	 fecharTransacao();
	 JOptionPane.showMessageDialog(null, "ok!");
	 return true;
	 } catch (Throwable e) {
	 return false;
	 }
	 }
	 
 public boolean getAlterar() {
	 try {
	 abrirTransacao();
	 session.update(t);
	 fecharTransacao();
	 return true;
	 } catch (Throwable e) {
	 return false;
	 }
	 }
	 public boolean getExcluir(Telefones tel) {
	 try {
	 abrirTransacao();
	 session.delete(t);
	 fecharTransacao();
	 return true;
	 } catch (Throwable e) {
	 return false;
	 }}}